package com.doit.spark.day01

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

/**
 * @DATE 2022/1/2/16:08
 * @Author MDK
 * @Version 2021.2.2
 * */
class MysqlIterator(val url:String, val username:String, val pwd:String) extends Iterator[User]{
    //注册驱动
  Class.forName("com.mysql.jdbc.Driver")
  //获取连接对象 执行SQL语句
  private val connection: Connection = DriverManager.getConnection(url, username, pwd)
  private val statement: PreparedStatement = connection.prepareStatement("select * from tb_user")
  private val set: ResultSet = statement.executeQuery()

  override def hasNext: Boolean = set.next()

  override def next(): User = {
    val id = set.getInt(1)
    val name = set.getString(2)
    val age = set.getInt(3)
    val gender = set.getString(4)
    User(id , name , age , gender )
  }
}
